package com.group15.phms;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;


import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class VitalSignsActivity extends Activity {
	String currentUser;
	SharedPreferences logins;
	ArrayList<VitalSigns> vList;
	DatabaseHelper db;

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_vital_signs);
        // Show the Up button in the action bar.
        setupActionBar();
        
        Context context = getApplicationContext();
		logins = getSharedPreferences("logins",MODE_PRIVATE);
		currentUser = logins.getString("currentUser","INVALID");
		db= new DatabaseHelper(getApplicationContext());
		//get VitalSignsController
		VitalSignsController vc = new VitalSignsController();
		//get vital signs info
		vList = vc.getVitalsList(context, currentUser, db);
		Collections.sort(vList);
		int size = vList.size();
		
		
		//format data to view vital signs
		      
        String[] months = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
		TextView t1, t2, t3, t4;
    	String date, chol,glu, bp;
    	
        //for(int i=0;i<5;i++){// day 1 column
    		int i=0;
    		Calendar c = Calendar.getInstance();
        	date = ""; chol = "";glu = ""; bp = "";
        	if(size >= 1){
        		c.setTime( vList.get(i).getDate() );
	        	date = months[c.get(Calendar.MONTH)] + " " + c.get(Calendar.DAY_OF_MONTH);
	        	chol = "" + vList.get(i).getCholesterol();
	        	glu = "" + vList.get(i).getBloodGlucose();
	        	bp = "" + vList.get(i).getSystolicBP() + "/" + vList.get(i).getDiastolicBP();
        	}
	        	t1=(TextView)findViewById(R.id.day0); t1.setText(date);
        		t2=(TextView)findViewById(R.id.c0); t2.setText(chol);	
        		t3=(TextView)findViewById(R.id.bp0); t3.setText(glu);
        		t4=(TextView)findViewById(R.id.g0); t4.setText(bp); 
        	
        	i=1;	
        	date = ""; chol = "";glu = ""; bp = "";
        	if(size >= 2){// day2 column
        		c.setTime(vList.get(i).getDate());
        		date = months[c.get(Calendar.MONTH)] + " " + c.get(Calendar.DAY_OF_MONTH);
            	chol = "" + vList.get(i).getCholesterol();
            	glu = "" + vList.get(i).getBloodGlucose();
            	bp = "" + vList.get(i).getSystolicBP() + "/" + vList.get(i).getDiastolicBP();
        	}
            	t1=(TextView)findViewById(R.id.day1); t1.setText(date);
        		t2=(TextView)findViewById(R.id.c1); t2.setText(chol);	
        		t3=(TextView)findViewById(R.id.bp1); t3.setText(glu);
        		t4=(TextView)findViewById(R.id.g1); t4.setText(bp); 
            i=2; date = ""; chol = "";glu = ""; bp = "";
        	if(size >= 3){// day3 column
        		c.setTime( vList.get(i).getDate());
        		date = months[c.get(Calendar.MONTH)] + " " + c.get(Calendar.DAY_OF_MONTH);
            	chol = "" + vList.get(i).getCholesterol();
            	glu = "" + vList.get(i).getBloodGlucose();
            	bp = "" + vList.get(i).getSystolicBP() + "/" + vList.get(i).getDiastolicBP();
        	}
            	t1=(TextView)findViewById(R.id.day2); t1.setText(date);
        		t2=(TextView)findViewById(R.id.c2); t2.setText(chol);	
        		t3=(TextView)findViewById(R.id.bp2); t3.setText(glu);
        		t4=(TextView)findViewById(R.id.g2); t4.setText(bp); 
        		
            i=3; date = ""; chol = "";glu = ""; bp = "";
        	if(size >= 4){ //day 4
        		c.setTime(vList.get(i).getDate());
        		date = months[c.get(Calendar.MONTH)] + " " + c.get(Calendar.DAY_OF_MONTH);
            	chol = "" + vList.get(i).getCholesterol();
            	glu = "" + vList.get(i).getBloodGlucose();
            	bp = "" + vList.get(i).getSystolicBP() + "/" + vList.get(i).getDiastolicBP();
        	}
            	t1=(TextView)findViewById(R.id.day3); t1.setText(date);
        		t2=(TextView)findViewById(R.id.c3); t2.setText(chol);
        		t3=(TextView)findViewById(R.id.bp3); t3.setText(glu);
        		t4=(TextView)findViewById(R.id.g3); t4.setText(bp); 
            i=4;
            date = ""; chol = "";glu = ""; bp = "";
        	if(size >= 5){//day 5 column
        		c.setTime(vList.get(i).getDate());
        		date = months[c.get(Calendar.MONTH)] + " " + c.get(Calendar.DAY_OF_MONTH);
            	chol = "" + vList.get(i).getCholesterol();
            	glu = "" + vList.get(i).getBloodGlucose();
            	bp = "" + vList.get(i).getSystolicBP() + "/" + vList.get(i).getDiastolicBP();
        	}
            	t1=(TextView)findViewById(R.id.day4); t1.setText(date);
        		t2=(TextView)findViewById(R.id.c4); t2.setText(chol);	
        		t3=(TextView)findViewById(R.id.bp4); t3.setText(glu);
        		t4=(TextView)findViewById(R.id.g4); t4.setText(bp); 
           
       // }       
        Button btn = (Button) findViewById(R.id.addvitalSigns);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            	Intent newIntent = new Intent(VitalSignsActivity.this, AddVitalSignsActivity.class);
				startActivity(newIntent);
            }
        });//end onClick
        
	}//end of onCreate

	
	//Set up the {@link android.app.ActionBar}.
	private void setupActionBar() { getActionBar().setDisplayHomeAsUpEnabled(true); }

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.vital_signs, menu);
		return true;
	}
	
	Intent intent;
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			NavUtils.navigateUpFromSameTask(this);
			return true;
		case R.id.action_logout:
			intent = new Intent(this, Login.class);
	    	startActivity(intent);
	    	finish();
			return true;
		case R.id.action_search:
			intent = new Intent(this, Search.class);
	    	startActivity(intent);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
